package com.zablert.lazyutil.utils

import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.PsiJavaFile
import com.intellij.psi.impl.file.PsiDirectoryFactory
import java.io.File

object VFileUtil {
    /**
     * If param 'name' is empty , use all path
     */
    @JvmStatic
    @JvmOverloads
    fun createFile(path: String, name: String = "") {
//        Log.d("createFile path = ${getPathName(path)}")
////        LocalFileSystem.getInstance().refreshFiles()
//        LocalFileSystem.getInstance().refreshAndFindFileByIoFile(
//            File(
//                getPathName(path,name)
//            )
//        )?.let {
////创建目录
//            val baseDir:PsiDirectory = PsiDirectoryFactory.getInstance(project).createDirectory(project.getBaseDir());
////创建Java文件
//            val psiFile :PsiJavaFile=  PsiFileFactory.getInstance(project).createFileFromText("", StdFileTypes.JAVA, "");
//        } ?: run {
//        }
    }

    private fun getPathName(path: String, name: String = ""): String {
        val pathEnd = path.endsWith("/")
        val nameStart = name.startsWith("/")
        if (name.isEmpty()) {
            if (pathEnd) {
                throw IllegalArgumentException("When argument 'file name' is empty , argument 'file path' must end with a file name")
            }
            return path
        } else {
            return if (pathEnd) {
                if (nameStart) {
                    "$path${name.replaceFirst("/", "")}"
                    //                    throw IllegalArgumentException("'file path' and 'file name' include the end of '/' and the beginning of '/', respectively")
                } else {
                    "$path$name"
                }
            } else {
                if (nameStart) {
                    "$path$name"
                } else {
                    "$path/$name"
                    //                    throw IllegalArgumentException("'file path' and 'file name' include the end of '/' and the beginning of '/', respectively")
                }
            }
        }

    }
}